package com.cash.app.utils

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore

object ImagePickerUtils {

    @SuppressLint("IntentReset")
    fun getImagePickerIntent(allowMultiple: Boolean = true, selectCount: Int = 3): Intent =
        Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI).apply {
            type = "image/*"
            if (allowMultiple) {
                putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
                putExtra(Intent.EXTRA_LOCAL_ONLY, true)
                putExtra("android.intent.extra.MIME_TYPES", arrayOf("image/*"))
                putExtra("max_select_count", selectCount)
            } else {
                putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
            }
        }

    fun handleImagePickerResult(data: Intent?, maxImages: Int): List<Uri> {
        val selectedImages = mutableListOf<Uri>()

        data?.let { intent ->
            if (intent.clipData != null) {
                val count = minOf(intent.clipData!!.itemCount, maxImages)
                for (i in 0 until count) {
                    val imageUri = intent.clipData!!.getItemAt(i).uri
                    selectedImages.add(imageUri)
                }
            } else if (intent.data != null) {
                selectedImages.add(intent.data!!)
            }
        }

        return selectedImages
    }
} 